/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ua.yandex.lockfree;

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 *
 * @author Neophron
 */
public class MyQueue {

    private final AtomicReference<BigInteger> number;
    private static final BigInteger TWO = new BigInteger("2");
 
    public MyQueue() {
        number = new AtomicReference<>(BigInteger.ONE);
    }

    public BigInteger next() {
        BigInteger res = null;
        for(;;){
            res = number.get();
            BigInteger newN = res.multiply(TWO);
            if (number.compareAndSet(res, newN)) {
                break;
            }
        }
        return res;
    }

}
